var WidgetStore = function()
{
  var _P = 
  {
    init : function(params) 
    {
      _P.params = params;
      _P.loadXml();
      $("#widget-loader").ajaxStop(function() 
      {
        _P.loadCategories();
        $(this).hide();
      });
    },

    params : null,

    categories : null,

    currentCategory: null,

    loadXml : function() 
    {
      $.ajax({
        type : "GET",
        url : _P.params.xmlPath+"categories.xml",
        dataType : "xml",
        success : function( data ) {
          _P.max = _P.params.perView;
          _P.parseCategories(data);
        }
      });
    },

    parseCategories : function(data)
    {
      _P.categories = new Array();
      $("category", data).each(function(i) 
      {
        var cat = new Category();
        cat.widgets = new Array();
        cat.name = $.trim($("name", this).text());
        cat.name = cat.name.substr(0, cat.name.indexOf("."));
        if ( cat.name.indexOf(" ") > -1 )
          cat.url = cat.name.substr(0, cat.name.indexOf(" "))+".xml";
        else
          cat.url = cat.name+".xml";

        $.ajax({
          type : "GET",
          url : _P.params.xmlPath+cat.url,
          dataType : "xml",
          success : function( data ) {
            _P.data = data;
            _P.max = _P.params.perView;
            _P.parseWidgets(data, cat);
          }
        });

        _P.categories.push(cat);
      });
    },

    parseWidgets : function(data, category)
    {
      $("widget", data).each(function(i) 
      {
        var widget = $.trim($("name", this).text());
        $.ajax({
          type : "GET",
          url : _P.params.xmlPath+widget+".xml",
          dataType : "xml",
          success : function( data ) {
            _P.data = data;
            _P.max = _P.params.perView;
            _P.parseWidget(data, category);
          }
        });
      });
    },

    parseWidget : function(data, category)
    {
      var widgets = new Array();
      $("widgetInfo", data).each(function(i) 
      {
        var widget = new Widget();
        widget.id           = $.trim($("id", this).text());
        widget.package      = $.trim($("fileName", this).text());
        widget.iconUrl      = $.trim($("iconFileName", this).text());
        widget.name         = $.trim($("name", this).text());
        widget.author       = $.trim($("author", this).text());
        widget.dateReleased = $.trim($("dateReleased", this).text());
        widget.version      = $.trim($("version", this).text());
        widget.description  = $.trim($("description", this).text());
        widget.price        = $.trim($("installPrice", this).text());
        widget.monthlyPrice = $.trim($("monthlyPrice", this).text());
        widget.rating = $.trim($("rating", this).text());
        widget.iconUrl = _P.params.xmlPath+widget.iconUrl;
        category.widgets.push(widget);
      });
    },

    loadCategories : function() 
    {
      $("#categories-list").empty();
      $(_P.categories).each(function(i) 
      {
        var category = this;
        $("#categories-list").append([
           "<li id='category_", i, "'>", category.name, "</li>"].join(""));
        $("#category_"+i).click(function() 
        { 
//           $(this).css("background-color", "#fff");
          _P.loadWidgets(category);
        });
        if ( i == 0 )
        {
//           $("#category_0").css("background-color", "#fff");
          _P.loadWidgets(category);
        }
      });
      $("#prev").click(function()
      {
        _P.browseWidgets("prev");
        return false;
      });

      $("#next").click(function() 
      {
        _P.browseWidgets("next" );
        return false;
      });
    },

    loadWidgets : function(category) 
    {
      _P.currentCategory = category;
      $("#widget-list").empty();
      $(category.widgets).each(function(i) 
      {
        $("#widget-list").append([
          "<div class='widget-item' id='widget_", i, "'>",
            "<img src='", this.iconUrl, "' class='widget-icon'/>",
            "<div class='name'>", this.name, "</div>",
            "<div class='version'>", this.version, "</div>",
            "<img src='images/", this.rating, "_star.gif' class='rating'/>",
            "<div class='description'>", this.description, "</div>",
            "<div class='author'>by ", this.author, "</div>",
          "</div>"].join(""));
      });
      _P.first = 0;
      _P.browseWidgets();
    },

    browseWidgets : function(browse) 
    {
      _P.count = _P.currentCategory.widgets.length;
      $("#widgets-header").empty().append("Browse "+_P.currentCategory.name);

      if ( browse == "prev" ) 
      {
        if ( _P.first == _P.count && (_P.count % _P.max > 0) ) 
          _P.first = _P.first - ((_P.count % _P.max) + _P.max);
        else 
          _P.first = _P.first - (_P.max * 2);
      }

      var range = _P.first + _P.max;
      var start = 1;

      if ( range > _P.max ) 
        start = ((range - _P.max) + 1);

      if ( _P.first == 0 )
        $("#prev").css("visibility", "hidden");
      else
        $("#prev").css("visibility", "visible");
      
      if ( range < _P.count )
        $("#next").css("visibility", "visible");
      else if ( range >= _P.count )
      {
        range = _P.count;
        $("#next").css("visibility", "hidden");
      }

      $(_P.currentCategory.widgets).each(function(i) 
      {
        if ( i >= _P.first && i < range )
          $("#widget_"+i).fadeIn("slow");
        else
          $("#widget_"+i).css("display", "none");
      });

      _P.first = range;
      $("#widgets-pager").html([
        start,
        " - ",
        range,
        " of ",
        _P.count].join(""));
    },

    first : 0,

    max : 0

  };

  return {
    init : function( params ) {
      _P.init( params );
    }
  };
}();










function Category()
{
  var name;
  var url;
  var widgets;
}

function Widget()
{
  var id;
  var package;
  var iconUrl;
  var name;
  var author;
  var dateReleased;
  var version;
  var description;
  var price;
  var monthlyPrice;
  var rating;
}